//
//  AppDelegate.m
//  MusicPlayer
//
//  Created by dlios on 15-5-21.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "AppDelegate.h"
#import "MusicLibraryViewController.h"
#import "LeftViewController.h"




#import "YRSideViewController.h"
#import <SDWebImage/UIImageView+WebCache.h>

#import "UMSocial.h"

#import "PlayMusicViewController.h"
#import "RecommendLatestModel.h"
#import "PlayMusicViewController.h"
#import "RecommendViewController.h"
#import "Singleton.h"
#import "pageListViewController.h"
#import "RecommendNewMusicViewController.h"
@interface AppDelegate ()<UITableViewDataSource, UITableViewDelegate>
@property (strong,nonatomic) YRSideViewController *sideViewController;
@property (nonatomic, retain) UIButton *button;
@property (nonatomic, retain) UIButton *nextButton;
@property (nonatomic, retain) UIView *listView;
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) UIButton *playButton;
@property (nonatomic, retain) UIView *backView;
@end

@implementation AppDelegate
- (void)dealloc
{
    [_window reloadInputViews];
    [_button release];
    [_nextButton release];
    [_listView release];
    [_tableView release];
    [_playView release];
    [_playButton release];
    [_backView release];
    [super dealloc];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    [self.window setBackgroundColor:[UIColor whiteColor]];
    [_window makeKeyAndVisible];
    [_window release];
    
    UIImage *image = [UIImage imageNamed:@"enter2.png"];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
    imageView.frame = [[UIScreen mainScreen] bounds];
    [self.window addSubview:imageView];
    [imageView release];
    
    [UIView animateWithDuration:2.5 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
        imageView.alpha = 0.4;
    } completion:^(BOOL finished) {
        [imageView removeFromSuperview];
    

    MusicLibraryViewController *musicLibrary = [[MusicLibraryViewController alloc] init];
        
    
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:musicLibrary];
    
    [musicLibrary release];
    


    
    LeftViewController *leftViewController=[[LeftViewController alloc]initWithNibName:nil bundle:nil];
   
    
    self.sideViewController=[[YRSideViewController alloc]initWithNibName:nil bundle:nil];
    _sideViewController.rootViewController= nav;
    _sideViewController.leftViewController=leftViewController;

    
    
    _sideViewController.leftViewShowWidth=200;
    _sideViewController.needSwipeShowMenu=true;//默认开启的可滑动展示
    
    [self.window setRootViewController:self.sideViewController];

        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        
        
        self.playView = [[UIView alloc] initWithFrame:CGRectMake(0, self.window.frame.size.height - self.window.frame.size.height / 667 * 70, self.window.frame.size.width, self.window.frame.size.height / 667 * 70)];
        [_window addSubview:_playView];
        [_playView setBackgroundColor:[UIColor whiteColor]];
        _playView.alpha = 0.9;
        [_playView release];
        
        
        self.button = [[UIButton alloc] initWithFrame:CGRectMake(self.window.frame.size.width / 4 * 1.8, self.window.frame.size.height / 667 * 5, self.window.frame.size.height / 667 * 50, self.window.frame.size.height / 667 * 50)];
        [_button setImage:[UIImage imageNamed:@"play.ico"] forState:UIControlStateNormal];
        [_button addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
        [_playView addSubview:_button];
        [_button release];
        
        self.nextButton = [[UIButton alloc] initWithFrame:CGRectMake(self.window.frame.size.width / 6 * 5, self.window.frame.size.height / 667 * 10, self.window.frame.size.height / 667 * 35, self.window.frame.size.height / 667 * 35)];
        [_nextButton setImage:[UIImage imageNamed:@"list.ico"] forState:UIControlStateNormal];
        [_nextButton addTarget:self action:@selector(nextAction1:) forControlEvents:UIControlEventTouchUpInside];
        [_playView addSubview:_nextButton];
        [_nextButton release];
        
        UIImageView *littleImageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.window.frame.size.width / 6 * 5, self.window.frame.size.height / 667 * 5, self.window.frame.size.height / 375 * 40, self.window.frame.size.height / 667 * 40)];
        [_playView addSubview:littleImageView];
        [littleImageView release];

        UIImageView *albumImageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.window.frame.size.width / 375 * 5, 5, self.window.frame.size.width / 375 * 50, self.window.frame.size.width / 375 * 50)];
        albumImageView.userInteractionEnabled = YES;
        albumImageView.clipsToBounds = YES;
        albumImageView.layer.cornerRadius = self.window.frame.size.height / 667 * 25;
        if ([defaults objectForKey:@"pic"] != nil) {
            [albumImageView sd_setImageWithURL:[defaults objectForKey:@"pic"] placeholderImage:nil];
        } else
        {
            [albumImageView setImage:[UIImage imageNamed:@"enter.jpg"]];
        }
        
        [self.playView addSubview:albumImageView];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(playAction)];
        [albumImageView addGestureRecognizer:tap];
        [tap release];
        
        UILabel *songInfo = [[UILabel alloc] initWithFrame:CGRectMake(self.window.frame.size.width / 375 * 60, 10, self.window.frame.size.width / 675 * 150, self.window.frame.size.width / 375 * 40)];
        songInfo.numberOfLines = 0;
        songInfo.font = [UIFont systemFontOfSize:self.window.frame.size.width / 375 * 16];
        [_playView addSubview:songInfo];
        
        Singleton *singleton = [Singleton shareManager];
  
        if ([defaults objectForKey:@"songName"]) {
            songInfo.text = [NSString stringWithFormat:@"%@", [defaults objectForKey:@"songName"]];
            
        } else {
            songInfo.text = @"快去点一首歌曲吧!";
        }
        
        [songInfo release];
        
         NSString *str = @"播放";
        if ([str isEqualToString:singleton.name]) {
            singleton.name = @"暂停";
            [self.button setImage:[UIImage imageNamed:@"play.ico"] forState:UIControlStateNormal];
//            [singleton playWithUrl:[defaults objectForKey:@"songUrl"]];
        } else if (singleton.name == nil) {
            singleton.name = @"第一次";
        }
        else
        {
            singleton.name = @"播放";
            [singleton.audio resume];
             [self.button setImage:[UIImage imageNamed:@"pause.ico"] forState:UIControlStateNormal];

        }
    
    }];

    
    [UMSocialData setAppKey:@"5566b32367e58e8d39001fe7"];
    

    return YES;
}


#pragma mark - 播放按钮的事件
- (void)buttonAction
{
    Singleton *singleton = [Singleton shareManager];
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString *str = @"播放";
    // 判断，如果单例属性等于“播放”，就暂停音乐，并停止动画；否则就继续播放，添加动画
    if ([str isEqualToString:singleton.name]) {
        singleton.name = @"暂停";
        [singleton.audio pause];
        [self.button setImage:[UIImage imageNamed:@"play.ico"] forState:UIControlStateNormal];
    } else if ([singleton.name isEqualToString:@"第一次"]) {
        singleton.name = @"暂停";
        [self.button setImage:[UIImage imageNamed:@"play.ico"] forState:UIControlStateNormal];
        [singleton playWithUrl:[defaults objectForKey:@"songUrl"]];
    }
    else
    {
        singleton.name = @"播放";
        [singleton.audio resume];
        [self.button setImage:[UIImage imageNamed:@"pause.ico"] forState:UIControlStateNormal];
        //        [self.pauseButton setBackgroundImage:[UIImage imageNamed:@"pause.ico"] forState:UIControlStateNormal];
    }

}

#pragma mark - 列表按钮的事件
- (void)nextAction1:(id)sender
{

    Singleton *singleton = [Singleton shareManager];
    if (singleton.array.count != 0) {
        self.backView = [[UIView alloc] initWithFrame:self.window.frame];
        self.backView.alpha = 0.4;
        [self.backView setBackgroundColor:[UIColor lightGrayColor]];
        [self.window addSubview:self.backView];
        
        // 给灰色背景添加点击事件
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)];
        [self.backView addGestureRecognizer:tap];
        [tap release];
        
        self.listView = [[UIView alloc] initWithFrame:CGRectMake(self.window.frame.size.width / 375 * 25, self.window.frame.size.height / 667 * 300, self.window.frame.size.width / 375 * 325, self.window.frame.size.height / 667 * 280)];
        self.listView.backgroundColor = [UIColor grayColor];
        self.listView.clipsToBounds = YES;
        self.listView.layer.cornerRadius = self.window.frame.size.width / 375 * 20;
        [self.window addSubview:self.listView];
        [self.listView release];
        
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.listView.frame.size.width, self.window.frame.size.height / 667 * 35)];
        titleLabel.textColor = [UIColor whiteColor];
        titleLabel.text = @"🎵 播放队列";
        [titleLabel setTextColor:[UIColor blackColor]];
        titleLabel.textAlignment = NSTextAlignmentCenter;
        [self.listView addSubview:titleLabel];
        [titleLabel release];
        
        self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, self.window.frame.size.height / 667 *  35, self.listView.frame.size.width, self.listView.frame.size.height - self.window.frame.size.height / 667 * 35 )];
        self.tableView.delegate = self;
        self.tableView.dataSource = self;
        [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
        
        [self.listView addSubview:_tableView];
        [_tableView release];
        
//        UIButton *exits = [[UIButton alloc] initWithFrame:CGRectMake(0, titleLabel.frame.size.height + self.tableView.frame.size.height, self.listView.frame.size.width, self.window.frame.size.height / 667 * 35)];
//        [exits addTarget:self action:@selector(exitsAction) forControlEvents:UIControlEventTouchUpInside];
//        [exits setTitle:@"返回" forState:UIControlStateNormal];
//        [exits setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//        [self.listView addSubview:exits];
//        [exits release];
    }
}

#pragma mark - 返回按钮的事件
- (void)exitsAction
{
    [self.listView removeFromSuperview];
    [self.backView removeFromSuperview];
}

#pragma mark - 跳转至播放界面的事件
- (void)playAction
{
    Singleton *singleton = [Singleton shareManager];
    if (singleton.array.count != 0) {
        RecommendLatestModel *model = [singleton.songListArray objectAtIndex:singleton.num];
        NSString *str = [singleton.array objectAtIndex:singleton.num];
        PlayMusicViewController *playMusic = [[PlayMusicViewController alloc] init];
        playMusic.songName = model.song_name;
        playMusic.singer = model.singer_name;
        playMusic.songUrl = str;
        playMusic.songArray = singleton.array;
        playMusic.songListArray = singleton.songListArray;
        playMusic.num = singleton.num;
        
        YRSideViewController *yrs = (YRSideViewController *)self.window.rootViewController;
        UINavigationController *nav = (UINavigationController *)yrs.rootViewController;
        [nav pushViewController:playMusic animated:YES];
        
        [playMusic release];
    }
    

}

#pragma mark - 灰色背景View的点击事件
- (void)tapAction
{
    [self.backView removeFromSuperview];
    [self.listView removeFromSuperview];
}

#pragma mark - 播放列表的歌曲数量
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    Singleton *singleton = [Singleton shareManager];
    return singleton.songListArray.count;
}

#pragma mark - 播放列表中存放的内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    Singleton *singleton = [Singleton shareManager];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    RecommendLatestModel *model = [[RecommendLatestModel alloc] init];
    model = [singleton.songListArray objectAtIndex:indexPath.row];
    NSString *str = [NSString stringWithFormat:@"%ld.%@ - %@", indexPath.row + 1, model.song_name, model.singer_name];
    cell.textLabel.text = str;
    return cell;
}

#pragma mark - 点击tableView的事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.backView removeFromSuperview];
    [UIView animateWithDuration:0.5 animations:^{
        
        Singleton *singleton = [Singleton shareManager];
        RecommendLatestModel *model = [singleton.songListArray objectAtIndex:indexPath.row];
        NSString *str = [singleton.array objectAtIndex:indexPath.row];
        PlayMusicViewController *playMusic = [[PlayMusicViewController alloc] init];
        playMusic.songName = model.song_name;
        playMusic.singer = model.singer_name;
        playMusic.songUrl = str;
        playMusic.songArray = singleton.array;
        playMusic.songListArray = singleton.songListArray;
        playMusic.num = indexPath.row;
        
        YRSideViewController *yrs = (YRSideViewController *)self.window.rootViewController;
        UINavigationController *nav = (UINavigationController *)yrs.rootViewController;
        [nav pushViewController:playMusic animated:YES];
        
        [self.listView removeFromSuperview];
        [playMusic release];
        [model release];
    }];
}
- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
